package com.zy.io;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/12 10:34
 **/
public class WriterTest {
    public void testMyInput() throws Exception {
        FileInputStream fis = new FileInputStream("d:/temp/test.txt");
        FileOutputStream fos = new FileOutputStream("d:/temp/test_1.txt");

        InputStreamReader isr = new InputStreamReader(fis, "GBK");
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");

        BufferedReader br = new BufferedReader(isr);
        BufferedWriter bw = new BufferedWriter(osw);

        String str = null;
        while ((str = br.readLine()) != null) {
            bw.write(str);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();
    }

    public void saveText() throws IOException {
        FileOutputStream fos = new FileOutputStream("d:/temp/test.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos,"GBK");
        outputStreamWriter.write("这是一句信息,云南昆明");
        outputStreamWriter.flush();
        outputStreamWriter.close();

    }
   public void inputStreaRea() throws IOException {
        /*本例从控制台接受输入，然后写入到文件中，直到用户输入"!!!"为止*/
        File file = new File("d:/temp/test.txt");   //创建文件对象
        if (!file.exists())                  //判断该文件是否存在，如果不存在则创建新文件
        {
            file.createNewFile();
        }
        FileWriter fr = new FileWriter(file);   //针对文件对象创建文件写入流对象
        BufferedWriter bw = new BufferedWriter(fr);   //为文件写入流建立缓冲流
//将控制台输入对象转化成字符流，并建立缓冲流
        BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
        String str = bin.readLine();    //接受从控制台输入的一行字符串
        while (!(str.equals("!!!")))    //如果输入"!!!"则代表输入结束
        {
            bw.write(str);              //将从控制台输入的字符串写入到文件中
            bw.newLine();               //换新行
            str = bin.readLine();       //再从控制台接受输入
            bw.flush();
        }
//关闭所有已经打开的流
        bw.close();
        fr.close();
        bin.close();

    }

    public void dataFileInputStream() throws IOException {
        DataInputStream in = new DataInputStream(new FileInputStream("d:/temp/test.txt"));
        DataOutputStream out = new DataOutputStream(new  FileOutputStream("d:/temp/test1.txt"));
        BufferedReader d  = new BufferedReader(new InputStreamReader(in));

        String count;
        while((count = d.readLine()) != null){
            String u = count.toUpperCase();
            System.out.println(u);
            out.writeBytes(u + "  ,");


        }
        d.close();
        out.close();
    }
    public  void test1(){
        FileInputStream in = null;
        DataInputStream date = null;
        File f = new File("d:/temp/text3.txt");
        try {
            in=new FileInputStream(f);
            date=new DataInputStream(in);
            System.out.println(date.readBoolean());
            System.out.println(date.readUTF());
            System.out.println(date.readChar());
            System.out.println(date.readInt());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public  void test2(){
        File file = new File("d:/temp/text3.txt");
        FileOutputStream out = null;
        DataOutputStream date = null;
        try {
            out = new FileOutputStream(file);
            date = new DataOutputStream(out);
            date.writeBoolean(true);
            date.writeUTF("我是中文");
            date.writeChar('d');
            date.writeInt(1);
            System.out.println("保存成功");

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    public static void main(String[] args) throws Exception {

        WriterTest t =new WriterTest();
t.test1();


//        t.saveText();
//        t.testMyInput();



//        //获取标准输入流
//        InputStream in = System.in;
//        //将字节流转换为字符流
//        InputStreamReader isr = new InputStreamReader(in);
//        //将字符流装饰
//        BufferedReader br = new BufferedReader(isr);
//        String string = null;
//        while ((string = br.readLine()) != null) {
//            if ("over".equals(string)) {
//                break;
//            }
//
////            abc
////            string.
////            System.out.println(new String(string.getBytes(StandardCharsets.UTF_8)));
//            byte[] bytes = string.getBytes("utf-8");
//
//            for (int i = 0; i < bytes.length; i++) {
//                System.out.println((int) bytes[i]);
//            }
//        }


//        File file = new File("d:/temp/test.txt");
//        //通过文件对象创建文件输出字符流对象
//        FileWriter fw = new FileWriter(file);
//        //将文件输出字符流包装成缓冲流
//        BufferedWriter bw = new BufferedWriter(fw);
//        bw.write(new String("大家好！".getBytes("GBK")));
////        bw.write("我正在学习Java。");
//        bw.newLine();    //换个新行
////        bw.write("请多多指教。");
////        bw.newLine();    //换新行
////        bw.write("Luckybug@21cn.com");
//        bw.close();      //关闭流
//        fw.close();      //关闭流


//        File file = new File("d:/temp/test.txt");
//        //通过文件对象创建文件读取流对象
//        FileReader fr = new FileReader(file);
//        //将文件读取流包装成缓冲读取流
//        BufferedReader br = new BufferedReader(fr);
//
//        String str;
//        while ((str = br.readLine()) != null)  //逐行读取数据
//        {
//            System.out.println(str);
//        }
//        br.close();   //关闭流
//        fr.close();   //关闭流

//        System.out.println();


    }
}
